import { requestClient } from '#/api/request'

import { downloadFileFromBlobPart } from '@vben/utils'

/**
 *  获取采购进度列表（实时取数）
 */
async function getPurchaseIndex(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/getPurchaseIndex', params)
}

/**
 *  获取采购进度列表（快照取数）
 */
async function getPurchaseIndex2(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/getPurchaseIndex2', params)
}

/**
 * 导出
 */
async function exportPurchaseProgress(params: any) {
  const res = await requestClient.post('/admin/MaterialPurchaseProgress/getPurchaseIndex', params, {
    responseType: 'blob', // 设置响应类型为 blob
    responseReturn: 'raw', // 返回原始响应而不是解析后的数据
  })

  const blob = new Blob([res.data], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  })
  downloadFileFromBlobPart({
    source: blob,
    fileName: '物料采购进度.xlsx',
  })
}

/**
 * 导出
 */
async function exportPurchaseProgress2(params: any) {
  const res = await requestClient.post('/admin/MaterialPurchaseProgress/getPurchaseIndex2', params, {
    responseType: 'blob', // 设置响应类型为 blob
    responseReturn: 'raw', // 返回原始响应而不是解析后的数据
  })

  const blob = new Blob([res.data], {
    type: 'application/vnd.openxmlformats-officedocument.spreadsheetml.sheet',
  })
  downloadFileFromBlobPart({
    source: blob,
    fileName: '货品采购进度.xlsx',
  })
}

/**
 *  保存采购进度数据
 */
async function savePurchaseProgress(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/savePurchaseProgress', params)
}

/**
 *  保存进度列配置
 */
async function saveProgressColumn(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/saveProgressColumn', params)
}

/**
 *  获取采购进度汇总数据
 */
async function getPurchaseSum(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/getPurchaseSum', params)
}

/**
 *  获取采购进度汇总数据（快照取数）
 */
async function getPurchaseSum2(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/getPurchaseSum2', params)
}

/**
 *  编辑采购详情
 */
async function editPurchaseDetail(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/editPurchaseDetail', params)
}

/**
 *  获取发货SKU详情
 */
async function getSendGoodsSku(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/getSendGoodsSku', params)
}

/**
 *  获取在途SKU详情
 */
async function getZtGoodsSku(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/getZtGoodsSku', params)
}

/**
 *  获取收货SKU详情
 */
async function getShouGoodsSku(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/getShouGoodsSku', params)
}

/**
 *  获取退货SKU详情
 */
async function getReturnGoodsSku(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/getReturnGoodsSku', params)
}

/**
 *  获取裁床SKU详情
 */
async function getCutedGoodsSku(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/getCutedGoodsSku', params)
}

/**
 *  获取分仓SKU详情
 */
async function getWarehouseSku(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/getWarehouseSku', params)
}

/**
 *  获取二次复期记录
 */
async function getSecondProcurementLog(params: any) {
  return requestClient.post('/admin/MaterialPurchaseProgress/get_second_procurement_log', params)
}

/**
 *  获取样式分类树
 */
async function getGoodsCategoryTree(params?: any) {
  return requestClient.get('/admin/goodsCategory/allIndex', params)
}

/**
 *  获取时间分类树
 */
async function getTimeCategoryTree(params?: any) {
  return requestClient.get('/admin/goodsTimeCategory/allIndex', params)
}

/**
 *  获取买手选项
 */
async function getGoodsHeelMan(params?: any) {
  return requestClient.get('/admin/goodsHeelMan/index', params)
}

/**
 *  获取供应商选项
 */
async function getSupplyOptions(params?: any) {
  return requestClient.get('/admin/factory/index', params)
}

/**
 *  获取仓库选项
 */
async function getWarehouseOptions(params?: any) {
  return requestClient.get('/admin/erpwarehouse/index', params)
}

/**
 *  获取跟单人选项
 */
async function getMerchandiserDict(params?: any) {
  return requestClient.post('/admin/dict/children', { key: 'merchUsers', ...params })
}

/**
 *  获取属性配置
 */
async function getAttrConfig(params: any) {
  return requestClient.post('/admin/attr/getAttrConfig', params)
}

/**
 *  获取详情属性值
 */
async function getDetailAttrValue(params: any) {
  return requestClient.post('/admin/attr/getDetailAttrValue', params)
}

/**
 *  保存属性值
 */
async function saveAttrValue(params: any) {
  return requestClient.post('/admin/attr/saveAttrValue', params)
}

/**
 *  标记完成
 */
export async function signFinish(params: any) {
  return requestClient.post('/admin/purchaseBomDetail/signFinish', params)
}

export {
  getPurchaseIndex,
  getPurchaseIndex2,
  savePurchaseProgress,
  saveProgressColumn,
  getPurchaseSum,
  getPurchaseSum2,
  editPurchaseDetail,
  getSendGoodsSku,
  getZtGoodsSku,
  getShouGoodsSku,
  getReturnGoodsSku,
  getCutedGoodsSku,
  getWarehouseSku,
  getSecondProcurementLog,
  getGoodsCategoryTree,
  getTimeCategoryTree,
  getGoodsHeelMan,
  getSupplyOptions,
  getWarehouseOptions,
  getMerchandiserDict,
  getAttrConfig,
  getDetailAttrValue,
  saveAttrValue,
  exportPurchaseProgress,
  exportPurchaseProgress2,
}
